<script setup>
import { ref } from 'vue'
import { Lock, User } from '@element-plus/icons-vue'

const isLogin = ref(true)
const formModel = ref({
    username: '',
    password: '',
    repassword: ''
})
const formRules = {
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
        { min: 6, max: 12, message: '用户名应为6~12位字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { pattern: /^\S{6,12}$/, message: '密码应为6~12位字符', trigger: 'blur' }
    ],
    repassword: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { pattern: /^\S{6,12}$/, message: '用户名应为6~12位字符', trigger: 'blur' },
        {
            validator: (rules, value, callback) => {
                if (value !== formModel.value.password) {
                    callback(new Error('密码不一致！'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ]
}
</script>
<template>
    <el-row class="page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form" v-if="isLogin">
            <el-form :model="formModel" size="large" ref="form" :rules="formRules">
                <el-form-item>
                    <h1>登录</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input :prefix-icon="User" type="text" placeholder="账号" v-model="formModel.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input :prefix-icon="Lock" type="password" placeholder="密码" v-model="formModel.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <div class="form-change">
                        <el-checkbox>记住密码</el-checkbox>
                        <el-link type="primary">忘记密码？</el-link>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" style="width: 100%" @click="">登录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-link type="primary" @click="isLogin = false">注册→</el-link>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="6" :offset="3" class="form" v-else>
            <el-form size="large" :model="formModel" :rules="formRules" ref="form">
                <el-form-item>
                    <h1>注册</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formModel.username" rules></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="formModel.password"></el-input>
                </el-form-item>
                <el-form-item prop="repassword">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码"
                        v-model="formModel.repassword"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space style="width: 100%" @click="">
                        注册
                    </el-button>
                </el-form-item>
                <el-form-item>
                    <el-link type="primary" @click="isLogin = true">登录→</el-link>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<style scoped lang="scss">
.page {
    height: 100vh;
    background-color: #fff;
}

.bg {
    background-color: aqua;
    border-radius: 0 30px 30px 0;
}

.form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
}

.form-change {
    display: flex;
    width: 100%;
    justify-content: space-between;
}
</style>